<template>
  <div>
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/platform/myCenter' }">个人中心</el-breadcrumb-item>
      <el-breadcrumb-item>实训任务</el-breadcrumb-item>
      <el-breadcrumb-item>我的任务</el-breadcrumb-item>
    </el-breadcrumb>
    <div style="margin-top: 12px;border: 1px solid #eeeeee;border-top: 3px solid #499aff">
      <div style="margin-left: 15px;float:left;margin-top: 8px;margin-bottom: 6px"><img style="align-content: center" src="../assets/stu/ico_list_today.png" height="32" width="32"/></div>
      <div style="float: left;vertical-align: middle;line-height: 50px;margin-left: 10px;color: #499aff">今日任务</div>
      <el-table :data="currentData" highlight-current-row v-loading="loading" style="width: 100%">
        <el-table-column
          type="index">
        </el-table-column>
        <el-table-column
          prop="name"
          label="任务名称"
          width="200"
        >
        </el-table-column>
        <el-table-column
          label="成员"
          width="100">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <li v-for="item in scope.row.member">
                {{item}}
              </li>
              <div slot="reference" class="name-wrapper">
                <el-tag>组</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="任务进度"
          width="100">
        </el-table-column>
        <el-table-column
          prop="taskTime"
          label="任务时间"
          width="100%">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button v-if="step.num < 5"
              size="small"
              @click="goLearn(scope.row)">学习</el-button>
            <el-button v-else
              size="small"
              @click="goLearn(scope.row)">查看</el-button>
            <!--<el-button-->
              <!--size="small"-->
              <!--@click="handleEdit(scope.row)" :disabled="step.num < 2 || step.num > 4">仿真</el-button>-->
            <!--<el-button-->
              <!--size="small"-->
              <!--@click="handleEdit(scope.row)" :disabled="step.num < 2|| step.num > 4">实训</el-button>-->
            <el-button
              size="small"
              type="danger"
              @click="goReport(scope.row)">
              实训报告</el-button>
            <el-button
              size="small"
              type="danger"
              @click="doFinished()"
              :disabled="step.num > 4">完成实验</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div style="margin-top: 12px;border: 1px solid #eeeeee;border-top: 3px solid #ff5d44">
      <div style="margin-left: 15px;float:left;margin-top: 8px;margin-bottom: 6px"><img style="align-content: center" src="../assets/stu/ico_list_history.png" height="32" width="32"/></div>
      <div style="float: left;vertical-align: middle;line-height: 50px;margin-left: 10px;color: #ff5d44">历史任务</div>
      <el-table :data="historyData" highlight-current-row v-loading="loading" style="width: 100%">
        <el-table-column
          type="index">
        </el-table-column>
        <el-table-column
          prop="name"
          label="任务名称"
          width="200"
          >
        </el-table-column>
        <el-table-column
          label="成员"
          width="100">
          <template slot-scope="scope">
            <el-popover trigger="hover" placement="top">
              <li v-for="item in scope.row.member">
                {{item}}
              </li>
              <div slot="reference" class="name-wrapper">
                <el-tag>组</el-tag>
              </div>
            </el-popover>
          </template>
        </el-table-column>
        <el-table-column
          prop="status"
          label="任务进度"
          width="100">
        </el-table-column>
        <el-table-column
          prop="taskTime"
          label="任务时间"
          width="100%">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button
              size="small"
              @click="goLearn(scope.row)">查看</el-button>
            <el-button
              size="small"
              type="danger"
              @click="goReport(scope.row)">
              实训报告</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
</template>

<script>
  import {getCurrentTask, getHistoryTask, getTask, updateStep, getStep} from '@/api/api'
  export default {
    data () {
      return {
        loading: false,
        currentData: [],
        historyData: [],
        step: this.$store.state.serverStep
//        finished: true  // 是否关闭完成实验按钮
      }
    },
    created () {
      this.initData()
      getStep().then(res => {
        this.$store.dispatch('serverStep', res.data.step)
      })
    },
    methods: {
      async initData () {
        this.loading = true
        try {
          getCurrentTask().then((res) => {
            this.currentData = res.data
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
        try {
          getHistoryTask().then((res) => {
            this.historyData = res.data
            this.loading = false
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      goLearn (row) {
        this.$store.dispatch('updateStep', 1)
        try {
          getTask(row.id).then((res) => {
            this.$store.dispatch('taskINFO', res.data)
            this.$router.push({path: '/platform/learn/explain'})
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      goReport (row) {
        try {
          getTask(row.id).then((res) => {
            this.$store.dispatch('taskINFO', res.data)
            this.$router.push({path: '/platform/learn/report'})
          })
        } catch (err) {
          console.log('获取数据失败', err)
        }
      },
      finished () {
        updateStep({step: 5}).then(res => {
          this.$store.dispatch('serverStep', res.data.step)
        })
      },
      doFinished () {
        this.$confirm('此操作将结束实验, 结束后将无法编辑实验报告，是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.finished()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消提交'
          })
        })
      }
    }
  }
</script>

<style scoped lang="less">
  div{
    .el-breadcrumb {
      margin-top: 10px;
    }
  }
</style>
